using System;
using System.Collections.Generic;
using UnityEngine;
using Voodoo.GameOps;
using Voodoo.Pattern;

public class DroneManager : MonoBehaviour
{
	public Item DroneItem;

	public DroneHandler DefaultSkin;

	private List<DroneHandler> _currentDrone = new List<DroneHandler>();

	private static DroneManager _instance;

	public static DroneManager Instance
	{
		get
		{
			if (!_instance)
			{
				_instance = UnityEngine.Object.FindObjectOfType<DroneManager>();
			}
			return _instance;
		}
	}

	private void Awake()
	{
		_instance = this;
	}

	private void Start()
	{
		if (!Preloader.ShopConfig.DrillingDrone)
		{
			base.gameObject.SetActive(value: false);
			return;
		}
		if (!InventoryManager.Instance.IsUnlocked(DroneItem))
		{
			base.gameObject.SetActive(value: false);
		}
		for (int i = 0; i < _currentDrone.Count; i++)
		{
			_currentDrone[i].DoReset();
		}
		GameManager instance = SingletonMB<GameManager>.Instance;
		instance.OnGameStart = (Action)Delegate.Combine(instance.OnGameStart, new Action(OnGameStarted));
		GameManager instance2 = SingletonMB<GameManager>.Instance;
		instance2.OnGameEnd = (Action)Delegate.Combine(instance2.OnGameEnd, new Action(OnGameFinished));
		InventoryManager instance3 = InventoryManager.Instance;
		instance3.OnSkinChanged = (Action)Delegate.Combine(instance3.OnSkinChanged, new Action(SetDroneSkin));
		InventoryManager instance4 = InventoryManager.Instance;
		instance4.OnItemPurchased = (Action)Delegate.Combine(instance4.OnItemPurchased, new Action(OnItemPurchased));
		SetDroneSkin();
	}

	private void OnDestroy()
	{
		if ((bool)InventoryManager.Instance)
		{
			InventoryManager instance = InventoryManager.Instance;
			instance.OnSkinChanged = (Action)Delegate.Remove(instance.OnSkinChanged, new Action(SetDroneSkin));
			InventoryManager instance2 = InventoryManager.Instance;
			instance2.OnItemPurchased = (Action)Delegate.Remove(instance2.OnItemPurchased, new Action(OnItemPurchased));
		}
		if ((bool)SingletonMB<GameManager>.Instance)
		{
			GameManager instance3 = SingletonMB<GameManager>.Instance;
			instance3.OnGameStart = (Action)Delegate.Remove(instance3.OnGameStart, new Action(OnGameStarted));
			GameManager instance4 = SingletonMB<GameManager>.Instance;
			instance4.OnGameEnd = (Action)Delegate.Remove(instance4.OnGameEnd, new Action(OnGameFinished));
		}
	}

	private void OnItemPurchased()
	{
		if (InventoryManager.Instance.IsUnlocked(DroneItem))
		{
			base.gameObject.SetActive(value: true);
			_currentDrone[0].DepleteFX.SetActive(value: false);
			_currentDrone[0].DepleteFX.SetActive(value: true);
			int inventoryCount = InventoryManager.Instance.GetInventoryCount(DroneItem);
			_currentDrone[0].transform.localScale = Vector3.Lerp(Vector3.one * 0.9f, Vector3.one * 1.5f, Mathf.Min(1f, (float)inventoryCount / 7f));
		}
	}

	public void OnGameStarted()
	{
		if (InventoryManager.Instance.GetInventoryCount(DroneItem) > 0)
		{
			for (int i = 0; i < _currentDrone.Count; i++)
			{
				_currentDrone[i].OnGameStarted(15 + InventoryManager.Instance.GetInventoryCount(DroneItem));
			}
		}
	}

	public void OnRVTaken()
	{
		base.gameObject.SetActive(value: true);
		for (int i = 0; i < _currentDrone.Count; i++)
		{
			_currentDrone[i].OnRVTaken();
		}
	}

	public void OnGameFinished()
	{
		for (int i = 0; i < _currentDrone.Count; i++)
		{
			_currentDrone[i].StopDrilling();
		}
		if (!InventoryManager.Instance.IsUnlocked(DroneItem))
		{
			base.gameObject.SetActive(value: false);
		}
	}

	public bool IsDrilling()
	{
		for (int i = 0; i < _currentDrone.Count; i++)
		{
			if (_currentDrone[i].IsDrilling())
			{
				return true;
			}
		}
		return false;
	}

	public void SetDroneSkin()
	{
		Item item = InventoryManager.Instance.CurrentlySelected(ItemType.Skin);
		DroneHandler original = DefaultSkin;
		if ((bool)item && (bool)item.Drone)
		{
			original = item.Drone;
		}
		for (int i = 0; i < _currentDrone.Count; i++)
		{
			UnityEngine.Object.Destroy(_currentDrone[i].gameObject);
		}
		_currentDrone.Clear();
		int inventoryCount = InventoryManager.Instance.GetInventoryCount(DroneItem);
		DroneHandler droneHandler = UnityEngine.Object.Instantiate(original, base.transform);
		droneHandler.DoReset();
		droneHandler.transform.localScale = Vector3.Lerp(Vector3.one * 0.9f, Vector3.one * 1.5f, Mathf.Min(1f, (float)inventoryCount / 7f));
		_currentDrone.Add(droneHandler);
	}
}
